//
//  JMRegistViewController.m
//  JMDome_1
//
//  Created by ios on 15/6/12.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMRegistViewController.h"
#import "JMRegistViewMode.h"
#import "JMUser.h"


@interface JMRegistViewController()<UITextFieldDelegate>

@property (nonatomic, strong) UIImageView *backImageView;

@property (nonatomic, strong) UIImageView *topImageView;
@property (nonatomic, strong) UILabel *topImageLabel;

@property (nonatomic, strong) UIImageView *registBackImageView;

@property (nonatomic, strong) UITextField *iphoneTextField;

@property (nonatomic, strong) UITextField *pwdTextField;

//@property (nonatomic, strong) UITextField *againPwdTextField;

@property (nonatomic, strong) UITextField *emailTextField;

@property (nonatomic, strong) UIButton *registbtn;

@end

@implementation JMRegistViewController

#pragma mark  创建控件
- (UIImageView *)backImageView
{
    if (!_backImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"login_regist.bundle/back_image"];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.backgroundColor = [UIColor clearColor];
        imageView.userInteractionEnabled = YES;
        _backImageView = imageView;
        [self.view addSubview:_backImageView];
    }
    return _backImageView;
}
- (UIImageView *)topImageView
{
    if (!_topImageView) {
        _topImageView = [[UIImageView alloc]init];
        _topImageView = [[UIImageView alloc]init];
        _topImageView.backgroundColor = [UIColor clearColor];
        _topImageView.image = [UIImage imageNamed:@"login_regist.bundle/topBack_image"];
        [self.backImageView addSubview:_topImageView];
    }
    return _topImageView;
}
- (UILabel *)topImageLabel
{
    if (!_topImageLabel) {
        UILabel* label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:14.0f];
        label.text = @"账号注册";
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        _topImageLabel = label;
        [self.topImageView addSubview:_topImageLabel];
    }
    return _topImageLabel;
}


- (UIImageView *)registBackImageView
{
    if (!_registBackImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"login_regist.bundle/regist_image"];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.backgroundColor = [UIColor clearColor];
        imageView.userInteractionEnabled = YES;
        _registBackImageView = imageView;
        [self.backImageView addSubview:_registBackImageView];
    }
    return _registBackImageView;
}
- (UITextField *)iphoneTextField
{
    if (!_iphoneTextField) {
        _iphoneTextField = [[UITextField alloc]init];
        _iphoneTextField.delegate =self;
        _iphoneTextField.placeholder = @"请输入手机号";
        _iphoneTextField.textColor = [UIColor whiteColor];
        [_iphoneTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        [self.registBackImageView addSubview:_iphoneTextField];
    }
    return _iphoneTextField;
}

- (UITextField *)pwdTextField
{
    if (!_pwdTextField) {
        _pwdTextField = [[UITextField alloc]init];
        _pwdTextField.delegate = self;
        _pwdTextField.placeholder = @"密码由6——18数字和字符组成";
        _pwdTextField.textColor = [UIColor whiteColor];
        [_pwdTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        [self.registBackImageView addSubview:_pwdTextField];

    }
    return _pwdTextField;
}

- (UITextField *)emailTextField
{
    if (!_emailTextField) {
        _emailTextField = [[UITextField alloc]init];
        _emailTextField.delegate =self;
        _emailTextField.placeholder = @"请输入邮箱方便找回密码";
        _emailTextField.textColor = [UIColor whiteColor];
        [_emailTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        [self.registBackImageView addSubview:_emailTextField];
    }
    return _emailTextField;
}

- (UIButton *)registbtn
{
    if (!_registbtn) {
        UIButton *button = [[UIButton alloc]init];
        [button setBackgroundImage:[UIImage imageNamed:@"login_regist.bundle/seleted_backImage@2x"] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor clearColor];
        [button setTitle:@"注册" forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(registbtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _registbtn = button;
        [self.registBackImageView addSubview:_registbtn];
    }
    return _registbtn;
}

#pragma mark  呈现界面
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navBackBtn.hidden =YES;
    [[UITextField appearance] setTintColor:[UIColor whiteColor]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHidden:) name:UIKeyboardDidHideNotification object:nil];
}

-(void)viewDidLoad{
    [super viewDidLoad];
    
    self.backImageView.frame =  self.view.bounds;
    self.topImageView.frame = CGRectMake(UI_SCREEN_WITH/2.0 - UI_SCREEN_WITH *0.25625/2.0, 26, UI_SCREEN_WITH *0.25625, UI_SCREEN_WITH *0.0875);
    self.topImageLabel.frame = CGRectMake(0, 0, self.topImageView.frameWidth, self.topImageView.frameHeight);
    CGFloat login ;
    if(iphone4){
        login = 60;
    }else if (iphone5){
        login = 100;
    }else if(iphone6){
        login = 120;
    }else{
        login = 140;
    }
    self.registBackImageView.frame = CGRectMake(0, login, UI_SCREEN_WITH, UI_SCREEN_WITH *0.671875);
    
    self.iphoneTextField.frame = CGRectMake(20, UI_SCREEN_WITH *0.09375, UI_SCREEN_WITH - 40, UI_SCREEN_WITH *0.125);
    self.pwdTextField.frame = CGRectMake(20, self.iphoneTextField.frameBottom, UI_SCREEN_WITH - 40, UI_SCREEN_WITH *0.125);
    self.emailTextField.frame = CGRectMake(20, self.pwdTextField.frameBottom, UI_SCREEN_WITH - 40, UI_SCREEN_WITH *0.125);
    
    self.registbtn.frame = CGRectMake(UI_SCREEN_WITH/2.0 - UI_SCREEN_WITH *0.234375/2.0, UI_SCREEN_WITH *0.49, UI_SCREEN_WITH *0.234375, UI_SCREEN_WITH *0.07031);

    [self.iphoneTextField becomeFirstResponder];
    [self.pwdTextField becomeFirstResponder];
    [self.emailTextField becomeFirstResponder];

}

#pragma mark  注册方法点击
-(void)registbtnClick:(id)sender{
    
    if(![JMTools isiPhoneNumber:self.iphoneTextField.text]){
        [JMTools showAlertView:self.view message:@"您输入的不是手机号"];
        return;
    }
//    if(![JMTools isRightpwd:self.pwdTextField.text]){
//        [JMTools showAlertView:self.view message:@"您输入的密码不符合格式"];
//        return;
//    }
    if(![JMTools isValidateEmail:self.emailTextField.text]){
        [JMTools showAlertView:self.view message:@"您输入的邮箱格式不正确"];
        return;
    }
    if(self.pwdTextField.text.length < 6){
        [JMTools showAlertView:self.view message:@"你输入的密码长度小于6位"];
        return;
    }
    [JMTools showAlertView:self.view];
    NSMutableDictionary *dict =[[NSMutableDictionary alloc]initWithCapacity:0];
    [dict setObject:self.iphoneTextField.text forKeyedSubscript:@"mobile"];
    [dict setObject:[[NSString stringWithFormat:@"%@%@",JMMd5Key,self.pwdTextField.text] JMMD5String] forKeyedSubscript:@"password"];
    [dict setObject:self.emailTextField.text forKeyedSubscript:@"email"];
    [JMRegistViewMode  fetchRegistMessageParametes:dict block:^(id data, NSError *error,id currData) {
        [JMTools hiddenAlerView:self.view];
        if([[data objectForKey:@"status"] integerValue] == 1){
             [JMTools showAlertView:self.view message:data[@"message"]];
            [self backBtnClick:nil];
        }else{
            [JMTools showAlertView:self.view message:data[@"message"]];
        }
        [JMTools hiddenAlerView:self.view];
    }];
}
#pragma mark -- jian pan huo dong
-(void)keyBoardShow:(NSNotification *)notion{
    NSDictionary *info = [notion userInfo];
    NSNumber *timeValue = info[UIKeyboardAnimationDurationUserInfoKey];
    [UIView animateWithDuration:[timeValue floatValue] animations:^{
        if(iphone4){
            self.registBackImageView.frame = CGRectMake(self.registBackImageView.frameLeft,self.registBackImageView.frameTop + 40,self.registBackImageView.frameWidth,self.registBackImageView.frameHeight);
        }
    } completion:^(BOOL finished) {
        
    }];
}

-(void)keyBoardHidden:(NSNotification *)notion{
    NSDictionary *info = [notion userInfo];
    NSNumber *timeValue = info[UIKeyboardAnimationDurationUserInfoKey];
    [UIView animateWithDuration:[timeValue floatValue] animations:^{
        if(iphone4){
            self.registBackImageView.frame = CGRectMake(self.registBackImageView.frameLeft,self.registBackImageView.frameTop + 40,self.registBackImageView.frameWidth,self.registBackImageView.frameHeight);
        }
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark  重写返回方法
-(void)backBtnClick:(id)sender{
    [self.iphoneTextField resignFirstResponder];
    [self.emailTextField resignFirstResponder];
    [self.pwdTextField resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
-(void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}
@end
